import storageUtil from './localstorage';

const storeUtil = {
  // Set a token with its name and value, with optional expiration time
  setInfo(key: string, user: any, expireInSeconds?: number) {
    if (expireInSeconds) {
      storageUtil.set({
        key: key, // Use token name as the key
        value: user, // Store the token object
        expire: `${expireInSeconds}s`, // Expire time in seconds
      });
    } else {
      // Store the token object without expiration
      storageUtil.set({
        key: key,
        value: user,
      });
    }
  },

  getInfo(key: string) {
    const userData = storageUtil.get(key);

    // If token exists and has not expired, return it, else return null
    if (userData && userData.value) {
      return userData.value; // { name, value }
    } else {
      return null;
    }
  },

  // Remove a token by its name
  removeInfo(key: string) {
    storageUtil.delete(key);
  },
};

export default storeUtil;
